
#include "index.hpp"
#include "Util.hpp"
#include <algorithm>
#include <json/json.h>

namespace ns_searcher
{
    class Searcher
    {
    private:
        ns_index::Index *index;

    public:
        Searcher() {}
        ~Searcher() {}

    public:
        void InitSearcher(const std::string &input)
        {
            //创建或者获取index对象
           
            index = ns_index::Index::GetInstance();
            if(index == nullptr) 
            {
                std::cerr << "index is null" << std::endl;
                return ;
            }
            //根据index对象建立索引
           
            index->BuildIndex(input);
            
        }

        void Search(const std::string&quarry,std::string*json_string)
        {
            //分词
            std::vector<std::string> words;
            ns_util::Ju::CutString(quarry, words);



            //根据分词查找倒排索引
            ns_index::InvertedList invertedlist_all;
            for (auto &word : words)
            {
                boost::to_lower(word);
                ns_index::InvertedList *inverted_list = index->GetInvertedList(word);

                if(inverted_list == nullptr) 
                {
                    continue;
                }

                std::sort(inverted_list->begin(), inverted_list->end(), [](const ns_index::InvertedElem &e1, const ns_index::InvertedElem &e2)
                          { return e1.weight > e2.weight; });

                invertedlist_all.insert(invertedlist_all.end(),inverted_list->begin(),inverted_list->end());

                Json::Value root;

                int count = 0;

                for(auto item:invertedlist_all)
                {

                    ns_index::DocInfo *doc = index->GetForwardIndex(item.doc_id);
                    if(doc == nullptr) 
                    {
                        continue;
                    }

                    Json::Value elem;

                    elem["title"] = doc->title;
                    elem["desc"] = DESC(doc->content,item.word);
                    elem["url"] = doc->url;

                    

                    root.append(elem);
                }

                Json::StyledWriter writer;
                *json_string = writer.write(root);
            }
        }

        std::string DESC(std::string& content, const std::string& word)
        {
            int pos = content.find(word);
            if (pos == std::string::npos)
            {
                return "none";
            }

            // 往前找50个，往后找100个
            int pre_step = 50;
            int next_step = 100;

            int start = 0;
            int end = content.size() - 1;

            if (pos - pre_step > 0)
            {
                start = pos - pre_step;
            }
            if (pos + next_step < end)
            {
                end = pos + next_step;
            }

            if (start >= end)
                return "none";

           // std::cout << content << std::endl;
            // std::cout << "start:"<<start << std::endl;
            // std::cout <<"end-start:"<< end - start << std::endl;
            std::string str = content.substr(start, end - start);
           // std::cout << str << std::endl;
            return str;
        }
    };
    
}
